<?php

namespace App\Exceptions;

use App\Helpers\ExceptionReport;
use Finecho\Logistics\Exceptions\InquiryErrorException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * 不会被报告的异常类型列表。
     *
     * @var array<int, class-string<\Throwable>>
     */
    protected $dontReport = [
        InvalidRequestException::class,
    ];


    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    public function render($request, Throwable $e)
    {
        // ajax请求我们才捕捉异常
        if ($request->ajax()){
            // 将方法拦截到自己的ExceptionReport
            $reporter = ExceptionReport::make($e);
            if ($reporter->shouldReturn()){
                return $reporter->report();
            }
            if(config('app.debug')) {
                // 开发环境，则显示详细错误信息
                return parent::render($request, $e);
            }else{
                // 线上环境,未知错误，则显示500
                return $reporter->prodReport();
            }
        }
        return parent::render($request, $e);
    }
}
